<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件
use think\Db;
use think\Session;
function addmoneylog($id,$about,$money,$action,$type='0',$arr=array()){//金额记录 
  
 $user=Db::name('member')->where('id',$id)->find();
 if($user){
     if($action=='+'){$date['yuanamount']=$user['amount']-$money;}
	 else{$date['yuanamount']=$user['amount']+$money;}
	 $date['houamount']=$user['amount'];
	 $date['notice']=$about;
	 $date['money']=$action.$money;
	 $date['addtime']=date("Y-m-d H:i:s");
	 $date['userid']=$id;
	 $date['type']=$type;
	 if($type=='1'){
	 $date['tzid']=$arr['id'];
	 $date['tzmoney']=$arr['tzmoney'];
	 }
	 $insert=Db::name('moneylog')->insert($date);
   
   }

}
function inviter($id){//会员名称

     
	  $user=Db::name('member')->where('id',$id)->find();
	  return $user['name'];
}
function membername($id){

$num=Db::name('member')->order('id desc')->field('name')->where('id',$id)->find();
return $num['name'];
}
function addnews($title,$about,$id=0){//添加短信 
  
 

    
	 $date['title']=$title;
	 $date['content']=$about;
	 $date['uid']=$id;
	 $date['addtime']=date("Y-m-d H:i:s");
	 
	 $insert=Db::name('news')->insert($date);
   
  

}

 function httpip() {
    //strcasecmp 比较两个字符，不区分大小写。返回0，>0，<0。
    if(getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) {
        $ip = getenv('HTTP_CLIENT_IP');
    } elseif(getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) {
        $ip = getenv('HTTP_X_FORWARDED_FOR');
    } elseif(getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) {
        $ip = getenv('REMOTE_ADDR');
    } elseif(isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) {
        $ip = $_SERVER['REMOTE_ADDR'];
    }
    $res =  preg_match ( '/[\d\.]{7,15}/', $ip, $matches ) ? $matches [0] : '';
    return $res;
    //dump(phpinfo());//所有PHP配置信息
}
function admindhl($id='user'){//后台导航 

  $arr= array(
	  
      "user"=>array(
		  "会员管理"=>array("user/userlist"=>"会员管理","user/xxlist"=>"平台公告","user/userdj"=>"会员等级管理"),
		 // "收货地址"=>array("user/usersh"=>"收货地址"),
		  "会员日志"=>array("user/moneylog"=>"余额日志","user/txlog"=>"提现日志","user/czlog"=>"充值日志",),
	   ),
      "basiczd"=>array(
		  "基本设置"=>array("basiczd/site"=>"站点设置"),
		  "充值管理"=>array("basiczd/payment"=>"充值管理"),
         "管理员管理"=>array("basiczd/admin"=>"管理员管理"),
		//  "项目配置"=>array("basiczd/publicd"=>"项目配置"),
		 
	   ),
	  "project"=>array(
		  "项目设置"=>array("project/label"=>"项目分类"),
		  "项目管理"=>array("project/release"=>"发布项目","project/zhongchouz"=>"项目管理"),
		  //"资金统计"=>array("project/statistics"=>"资金统计"),
		    "订单管理"=>array("project/order"=>"订单管理"),
	   ),
	  /*"module"=>array(
		  "反馈管理"=>array("module/feedback"=>"反馈管理"),
		 
	   ),*/
	   "operate"=>array(
		  "文章管理"=>array("operate/listf"=>"分类列表","operate/article"=>" 文章管理"),
		  
	   ),

     
  
  );
  if(empty($arr[$id])){
  return array();
  }
  return $arr[$id];


}
function cheshang_id($id,$arr){

return $arr[$id];
}

function projectfh(){//整体分红
	$num=0;
  $date['a.stater']=0;
  $date['a.fkdate']=['<=',date("Y-m-d H:i:s")];
   $config= Db::name('config')->find();
  $arr=Db::name('orderlist')->alias('a')->join('__PROJECT__ w','a.projectid=w.id')->where($date)->field('a.*,w.title,w.fenhong,w.fkfs,w.days')->select();//dump($arr);die;
  foreach($arr as $va){
      $money=$va['money']*$va['fenhong']/100;//单天金额
	 
	  $up=array();
	  if($va['fkfs']=='1'){$money=$money*$va['days'];}//周期结算金额
	  if($va['fkdate']<=date("Y-m-d H:i:s")){
	    if($va['yfhnum']>=$va['fhnum']){//分红次数已经用完返本金
		   $money=$va['money'];
		   $up['stater']='1';
		   $ab="项目：".$va['title']."到期本金返还";
            if($config['fhbj']!='0'){//本金返还间隔天数不为0
			   $fktime=date("Y-m-d H:i:s",strtotime($va['fkdate']."+1 day"));
			   if($fktime>date("Y-m-d H:i:s")){//返本金未到时间
			   $money=0;
			   }
			}
		}else{
		   $ab="项目:".$va['title']."到期分红";
		   $up['yfhnum']=$va['yfhnum']+1;//修改分红次数
		  if($up['yfhnum']!=$va['fhnum']) $up['fkdate']=date("Y-m-d H:i:s",strtotime($va['fkdate']."+1 day"));//修改分红时间
		   
		}
		
		
		
        if($money>0){
			 Db::startTrans();//事物开始
		  try{
		     Db::name('member')->where('id',$va['userid'])->setInc('amount',$money);//添加金额
			 addmoneylog($va['userid'],$ab,$money,'+');//添加金额记录
             Db::name('orderlist')->where('id',$va['id'])->update($up);//修改订单
			 $num++;
			   Db::commit();//提交事件
           
      
	       }catch (\Exception $e) {
			  
			  
			  // 回滚事务
			  
			   Db::rollback();
			   
			 return "未知错误！";
			}
		
		}


	  }

  }

  return "成功分红".$num."个！";
   
}
function paytype($type,$arr){
	if(!empty($arr[$type]))
   return $arr[$type];
      else return "线下充值";
}
function statetype($type){
  if($type=='1'){
   return "<font color='green'>成功</font>";
  }elseif($type=='2'){
  return "<font color='red'>失败</font>";
  }else{
	  return "<font color='red'>待处理</font>";
  }

}

function web($id='user'){//前台跟人中心

  $arr= array(
	  
      "user"=>array(
		  "会员中心"=>array("user/userzl"=>"个人资料","user/moneylog"=>"资金日志","user/czlog"=>"充值记录","user/txlog"=>"提现记录","user/tglj"=>"推广链接","user/wdtd"=>"我的团队"),
		
	   ),
      "about"=>array(
		  "消息管理"=>array("about/index"=>"消息通知"),
		
		 
	   ),
	  "usersz"=>array(
		  "项目设置"=>array("user/userzlxg"=>"修改资料","user/userpwd"=>"修改密码"),
		  
	   ),
	    "project"=>array(
		  "项目管理"=>array("user/orderlist"=>"已参与众筹"),
		  
	   ),
	   
	  

     
  
  );
  if(empty($arr[$id])){
  return array();
  }
  return $arr[$id];


}


function mobilexx($mo){
return substr($mo,0,3)."***".substr($mo, -3);

}
 function upload($name){
    // 获取表单上传文件 例如上传了001.jpg
    $file = request()->file($name);
    // 移动到框架应用根目录/public/uploads/ 目录下
	if($file){
    $info = $file->validate(['ext'=>'jpg,png,gif'])->move(ROOT_PATH . APP_UPLOADSS);
    if($info){
        
        return  $info->getSaveName();
         
    }else{
        
        return array('ste'=>'1','error'=>$file->getError());
    }
	}else{
	return '';
	}
}
